package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author RXC, 曾释天
 * @since 2022/07/09 9:17
 */
@RestController
@RequestMapping("shopping")
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;

    @Autowired
    OrderConverter orderConverter;

    @PostMapping("order")
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        // 获取用户信息
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        // 将用户id封入request
        Long uid = Long.parseLong(jsonObject.get("uid").toString());
        request.setUserId(uid);
        // // 将用户名也封装入request
        // String username = jsonObject.get("username").toString();
        // request.setUserName(username);

        // 设置uniqueKey
        request.setUniqueKey(UUID.randomUUID().toString());

        CreateOrderResponse response = orderCoreService.createOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("order")
    public ResponseData getOrder(OrderListRequest request, HttpServletRequest servletRequest) {
        // 获取用户信息
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        // 将用户id封入request
        Long uid = Long.parseLong(jsonObject.get("uid").toString());
        request.setUserId(uid);

        OrderListResponse response = orderQueryService.orderList(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            FinalOrderListResponse finalOrderListResponse = new FinalOrderListResponse();
            finalOrderListResponse.setData(response.getDetailInfoList());
            finalOrderListResponse.setTotal(response.getTotal());

            return new ResponseUtil<>().setData(finalOrderListResponse);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @GetMapping("order/{id}")
    public ResponseData getOrderById(@PathVariable("id") String id, HttpServletRequest servletRequest) {
        // // 获取用户信息
        // String userInfo = servletRequest.getHeader("user_info");
        // JSONObject jsonObject = JSON.parseObject(userInfo);
        // // 取出用户id和用户名
        // Long uid = Long.parseLong(jsonObject.get("uid").toString());
        // String username = jsonObject.get("username").toString();

        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(id);

        OrderDetailResponse response = orderQueryService.orderDetail(request);
        OrderDetailDto orderDetailDto = orderConverter.orderDetailResp2dto(response);
        // 避免空指针
        if (response != null) {
            orderDetailDto.setTel(response.getOrderShippingDto().getReceiverPhone());
            orderDetailDto.setStreetName(response.getOrderShippingDto().getReceiverAddress());
        }

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(orderDetailDto);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("order/{id}")
    public ResponseData delete(@PathVariable("id") String id) {
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id);
        DeleteOrderResponse response = orderCoreService.deleteOrder(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData("成功");
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest request) {
        CancelOrderResponse response = orderCoreService.cancelOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
